package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.UserDao;
import com.itheima.domain.Permission;
import com.itheima.domain.UserInfo;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {

    @Override
    public List<UserInfo> findAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<UserInfo> all = mapper.findAll(null);
        sqlSession.close();
        return all;
    }

    @Override
    public UserInfo findById(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        UserInfo userIn = mapper.findById(id);
        sqlSession.close();
        return userIn;
    }

    @Override
    public void save(UserInfo userInfo) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        String id = UUID.randomUUID().toString();
        userInfo.setId(id);
        //密码必须经过加密处理MD5加密
        userInfo.setPassword(MD5Util.md5(userInfo.getPassword()));
        mapper.save(userInfo);
        sqlSession.close();

    }

   /* @Override
    public void update(UserInfo userInfo) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.update(userInfo);
        sqlSession.close();
    }*/

    @Override
    public PageInfo<UserInfo> findAll(int page, int size,String username) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        PageHelper.startPage(page, size);
        List<UserInfo> all = mapper.findAll(username);
        PageInfo pageInfo = new PageInfo(all);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public void deleteAll(String[] ids) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);
        dao.deleteAll(ids);
        sqlSession.close();
    }

    @Override
    public void updateStatus(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);
        UserInfo user = dao.findById(id);
        int status = user.getStatus() == 1 ? 0 : 1;
        dao.updateStatus(id, status);
        sqlSession.clearCache();
    }

    @Override
    public void updateRole(String userId, String[] roleIds) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao dao = sqlSession.getMapper(UserDao.class);
        dao.deleteUserRole(userId);
        for (String roleId : roleIds) {
            dao.updateRole(userId, roleId);
        }
    }

    @Override
    public UserInfo login(String username, String password) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        UserInfo userInfo = mapper.findUseAndPwd(username, password);
        return userInfo;
    }

    @Override
    public List<Permission> findPermissionByUserId(String userId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        List<Permission> permissionByUserId = mapper.findPermissionByUserId(userId);
        return permissionByUserId;
    }


}
